﻿using System;
using System.Windows.Forms;
using System.Xml;
using System.IO;
//using System.Reflection;

namespace AutoMerge
{
    public partial class Form1 : Form
    {
        string branchPath;
        string environmentPath;
        string destination;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {                
                XmlDocument xmldoc = new XmlDocument();
                xmldoc.Load(@".\Configure.xml");

                XmlNodeList xnl = xmldoc.SelectNodes("//configuration/branches/branch");
                foreach (XmlNode xn1 in xnl)
                {
                    this.branchName.Items.Add(((XmlElement)xn1).InnerText);
                }
                this.branchName.SelectedIndex = 0;

                xnl = xmldoc.SelectNodes("//configuration/environments/environment");
                foreach (XmlNode xn2 in xnl)
                {
                    this.EnvironmentName.Items.Add(((XmlElement)xn2).InnerText);
                }
                this.EnvironmentName.SelectedIndex = 0;
            }
            catch (System.IO.FileNotFoundException)
            {
                showMessage.AppendText("Error: Can not find the configure file..." + "\r\n");
            }
            catch (Exception)
            {
                showMessage.AppendText("Error occurs when parsing Configure.xml..." + "\r\n");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                button1.Enabled = false;

                //get file from TFS, if exception captured, notify user and stop application
                GetFileFromTFS.get(branchPath, showMessage);

                //the path of two manifest file
                string machineFilePath = @".\output\MachineManifest.config";
                string roleFilePath = @".\output\RoleManifest.config";
                FileInfo fileInfo1 = new System.IO.FileInfo(machineFilePath);
                FileInfo fileInfo2 = new System.IO.FileInfo(roleFilePath);

                //remove the read only attribute, so that tool can update the file 
                fileInfo1.IsReadOnly = false;
                fileInfo2.IsReadOnly = false;

                //the application server and database server name
                string siteServer = "rr1nbc" + environmentPath.ToLower() + "web3";
                string dataServer = "rr1nbc" + environmentPath.ToLower() + "sql1";

                UpdateMachineManifest.update(machineFilePath, siteServer, dataServer, showMessage);
                UpdateRoleManifest.update(roleFilePath, environmentPath, showMessage);

                //revert the read only attribute 
                fileInfo1.IsReadOnly = true;
                fileInfo2.IsReadOnly = true;

                if (copyto.Checked == true)
                {
                    CopyFilesToDest.copyTo(destination, showMessage);
                }

                else
                {
                    showMessage.AppendText("\r\n");
                    showMessage.AppendText(@"Finished, the xmls are already at .\output\");
                }
            }
            catch (Exception ex)
            {
                if (ex.ToString() == "custom")
                {
                    return;
                }
                else
                {
                    showMessage.AppendText(ex.ToString());
                }
            }
            finally
            {
                button1.Enabled = true;
            }
        }

        private void branchName_SelectedIndexChanged(object sender, EventArgs e)
        {
            branchPath = this.branchName.SelectedItem.ToString();
        }

        private void EnvironmentName_SelectedIndexChanged(object sender, EventArgs e)
        {
            environmentPath = this.EnvironmentName.SelectedItem.ToString().ToLower();
        }

        private void destin_TextChanged(object sender, EventArgs e)
        {
            destination = this.destin.Text;
        }

        private void copyto_CheckStateChanged(object sender, EventArgs e)
        {
            if (destin.Enabled)
            {
                destin.Enabled = false;
            }
            else
            {
                destin.Enabled = true;
            }
        }
    }
}
